package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品信息： spu
     * @param key       查询关键字
     * @param page      当前页
     * @param rows      页大小
     * @param saleable  上下架
     * @return          返回分页对象： spuDTO
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageResult<SpuDTO>> querySpuByPage(
            @RequestParam(value="key",required = false) String key,
            @RequestParam(value="page",defaultValue = "1") Integer page,
            @RequestParam(value="rows",defaultValue = "5") Integer rows,
            @RequestParam(value="saleable",required = false) Boolean saleable
    ){
        PageResult<SpuDTO> result = goodsService.querySpuByPage(key, page, rows, saleable);
        return ResponseEntity.ok(result);
    }

    /**
     * 保存商品数据
     * @param spuDTO    spu的dto对象
     * @return          没有返回值
     */
    @PostMapping("/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 商品的上下架修改
     * @param id            SPU的id
     * @param saleable      上下架的boolean值
     * @return              无
     */
    @PutMapping("/spu/saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable){
        goodsService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spu的id查询spu详情信息
     * @param spuId spu的id
     * @return  spuDetail的DTO
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailBySpuId(@RequestParam("id") Long spuId){
        SpuDetailDTO spuDetailDTO = goodsService.querySpuDetailBySpuId(spuId);
        return ResponseEntity.ok(spuDetailDTO);
    }

    /**
     * 根据spu的id查sku集合
     * @param spuId   spu的id
     * @return        sku集合
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuListBySpuId(@RequestParam("id") Long spuId){
        List<SkuDTO> skuList = goodsService.querySkuListBySpuId(spuId);
        return ResponseEntity.ok(skuList);
    }

    /**
     * 更新商品
     * @param spuDTO    spudto封装了所有的数据
     * @return  没有返回值
     */
    @PutMapping("/goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据spuid查询spu
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id){
        SpuDTO spuDTO = goodsService.querySpuById(id);
        return ResponseEntity.ok(spuDTO);
    }

    /**
     * 根据sku的id集合查询sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids){
        List<SkuDTO> skuDTOList = goodsService.querySkuListByIds(ids);
        return ResponseEntity.ok(skuDTOList);
    }

    /**
     * 扣除商品的库存
     * @param cartsMap  购物车map： key是skuid； value是扣除的数量
     * @return          没有返回值
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> minusStock(@RequestBody Map<Long, Integer> cartsMap){
        goodsService.minusStock(cartsMap);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
